//
//  AYMPersonalMSGController.m
//  QuickCure
//
//  Created by 徐绩 on 16/5/7.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "AYMPersonalMSGController.h"
#import "AYMPersonalHeadView.h"
#import "AYMProvinceView.h"
#import "AYMPerosnalMSGView.h"

@interface AYMPersonalMSGController ()<UIPickerViewDataSource,UIPickerViewDelegate,AYMPerosnalMSGViewDelegate,AYMProvinceViewDelegate>

@property (nonatomic,strong) NSArray *titleLabelArr;
@property (nonatomic,weak)   UILabel *heightLabel;
@property (nonatomic,weak)   UILabel *weightLabel;
@property (nonatomic,weak)   UILabel *provinceLabel;
/// 省份plist文件
@property (nonatomic,strong) NSMutableArray *provincesPlist;
@property(nonatomic,weak)    AYMProvinceView *provinceView1;
@property (nonatomic,weak)   AYMPerosnalMSGView *popView;
@property(nonatomic,strong)  UITableViewCell *selectCell;
@property (nonatomic,weak)   UILabel *alertProvinceLabel;

@end
static NSString *identifier = @"personalCell";
@implementation AYMPersonalMSGController

-(NSMutableArray *)provincesPlist{
    
    if (_provincesPlist==nil) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"province.plist" ofType:nil];
        
        NSArray *arrM = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *arrMutable = [NSMutableArray array];
        for (NSDictionary *dict in arrM) {
            
            [arrMutable addObject:dict[@"name"]];
        }
        
        _provincesPlist = arrMutable;
    }
    
    
    return _provincesPlist;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人信息修改";
    self.titleLabelArr = @[@"身高",@"体重",@"省份"];
    
    [self setupUI];
}

#pragma mark - 布局界面
-(void)setupUI{

    AYMPersonalHeadView *headView = [[AYMPersonalHeadView alloc]initWithFrame:CGRectMake(0, 0, screenW, 125)];
    self.tableView.tableHeaderView = headView;

    UIView *footerView = [[UIView alloc]init];
    self.tableView.tableFooterView = footerView;

}

#pragma mark - tableView数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == self.provinceView1.provinceTableView) {
        
        return self.provincesPlist.count;
    }else{
        return 3;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.provinceView1.provinceTableView) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellPrivoce"];
        if (cell==nil) {
            
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellPrivoce"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.detailTextLabel.font = [UIFont systemFontOfSize:leftViewLoginFont];
        UILabel *alertProvinceLabel = cell.detailTextLabel;
        self.alertProvinceLabel = alertProvinceLabel;
        alertProvinceLabel.text = self.provincesPlist[indexPath.row];
        alertProvinceLabel.textColor = [UIColor blackColor];
        cell.imageView.image = [UIImage imageNamed:@"illness_rb_img_nor.png"];
        return cell;
        
        
    }else{
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc ]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = [UIFont systemFontOfSize:personalCellFont];
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.textLabel.text = self.titleLabelArr[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UserData *userData = [UserData sharedInstance];
        [userData loadUserInfoFromSanBox];
        if (indexPath.row == 0) {
            
            UILabel *heightLabel = cell.detailTextLabel;
            self.heightLabel = heightLabel;
            heightLabel.text = userData.height;
            
        }else if (indexPath.row == 1){
            
            UILabel *weightLabel = cell.detailTextLabel;
            self.weightLabel = weightLabel;
            weightLabel.text = userData.weight;
            
        }else{
            
            UILabel *provinceLabel = cell.detailTextLabel;
            self.provinceLabel = provinceLabel;
            provinceLabel.text = userData.address;
        }
        return cell;
    }
}


#pragma mark - 选中cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.provinceView1.provinceTableView) {
        
        self.selectCell.imageView.image = [UIImage imageNamed:@"illness_rb_img_nor.png"];
        
        self.selectCell = [tableView cellForRowAtIndexPath:indexPath];
        
        [tableView cellForRowAtIndexPath:indexPath].imageView.image = [UIImage imageNamed:@"illness_rb_img_sel.png"];
        
        self.alertProvinceLabel = self.selectCell.detailTextLabel;
        
    }else{
        
        self.tableView.scrollEnabled = NO;
        if (indexPath.row == 0) {
            
            //设置身高
            [self setupAlertHeightView];            
        }else if(indexPath.row == 1){
            
            //设置体重
            [self setupAlertWeightView];
        }else{
            
            //设置省份
            [self setupAlertProvinceView];
        }
    }
}

#pragma mark - pickerView数据源方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if (component == 0) {
        return 3;
    }else{
        return 10;
    }
}

#pragma mark - pickerView代理方法
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    UILabel *label = [[UILabel alloc]init];
    label.text = [NSString stringWithFormat:@"%ld",row];
    label.textColor = [UIColor orangeColor];

    return label;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

    return 20;
}


#pragma mark - 点击按钮代理
//身高体重提交
-(void)perosnalMSGView:(AYMPerosnalMSGView *)personalMSGView{
    
    
    NSString *comp0 = [NSString stringWithFormat:@"%ld",[self.popView.pickView selectedRowInComponent:0]];
    NSString *comp1 = [NSString stringWithFormat:@"%ld",[self.popView.pickView selectedRowInComponent:1]];
    NSString *comp2 = [NSString stringWithFormat:@"%ld",[self.popView.pickView selectedRowInComponent:2]];
    NSString *heightTitle = [NSString stringWithFormat:@"%@%@%@cm",comp0,comp1,comp2];
    NSString *weightTitle = [NSString stringWithFormat:@"%@%@%@kg",comp0,comp1,comp2];
    //开头数字是0则不显示
    NSString *heightTitle2 = [NSString stringWithFormat:@"%@%@cm",comp1,comp2];
    NSString *weightTitle2 = [NSString stringWithFormat:@"%@%@kg",comp1,comp2];
    
    UserData *userDate = [UserData sharedInstance];
    
    [self perosnalMSGViewCancelBtn:self.popView];
    if (self.tableView.indexPathForSelectedRow.row == 0) {
        
        if ([self.popView.pickView selectedRowInComponent:0] == 0) {
            
            self.heightLabel.text = heightTitle2;
        }else{
            
            self.heightLabel.text = heightTitle;
        }
        userDate.height = self.heightLabel.text;

        
    }else if (self.tableView.indexPathForSelectedRow.row == 1){
        
        if ([self.popView.pickView selectedRowInComponent:0] == 0) {
            
            self.weightLabel.text = weightTitle2;
        }else{
            self.weightLabel.text = weightTitle;
        }
        userDate.weight = self.weightLabel.text;
    }
    [userDate saveUserInfoSanBox:userDate];
    self.tableView.scrollEnabled = YES;
}

//身高体重取消
-(void)perosnalMSGViewCancelBtn:(AYMPerosnalMSGView *)personalMSGView{
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.popView.alpha = 0;
    } completion:^(BOOL finished) {
        
        [self.popView removeFromSuperview];
        
    }];
    self.tableView.scrollEnabled = YES;
}

//省份提交
-(void)provinceViewCommit:(AYMProvinceView *)provinceView{

     UserData *userData = [UserData sharedInstance];
    [self provinceViewCancel:provinceView];
    self.provinceLabel.text = self.alertProvinceLabel.text;
    userData.address = self.provinceLabel.text;
    [userData saveUserInfoSanBox:userData];

    self.tableView.scrollEnabled = YES;
}

//省份取消
-(void)provinceViewCancel:(AYMProvinceView *)provinceView{
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.provinceView1.alpha = 0;
    } completion:^(BOOL finished) {
        
        [self.provinceView1 removeFromSuperview];
        
    }];
    self.tableView.scrollEnabled = YES;
}

#pragma mark - 设置三个弹框
//身高
-(void)setupAlertHeightView{

    AYMPerosnalMSGView *popView = [[AYMPerosnalMSGView alloc]initWithFrame:self.view.bounds];
    self.popView = popView;
    [self.view addSubview:popView];
    popView.delegate = self;
    popView.pickView.delegate = self;
    popView.pickView.dataSource = self;
    

}
//体重
-(void)setupAlertWeightView{

    AYMPerosnalMSGView *popView = [[AYMPerosnalMSGView alloc]initWithFrame:self.view.bounds];
    self.popView = popView;
    popView.topLabel.text = @"设置体重(kg)";
    [self.view addSubview:popView];
    popView.delegate = self;
    popView.pickView.delegate = self;
    popView.pickView.dataSource = self;
}
//省份
-(void)setupAlertProvinceView{

    AYMProvinceView *provinceView = [[AYMProvinceView alloc]initWithFrame:self.view.bounds];
    self.provinceView1 = provinceView;
    [self.view addSubview:provinceView];
    provinceView.provinceTableView.dataSource = self;
    provinceView.provinceTableView.delegate = self;
    provinceView.delegate = self;
}
@end
